package com.crecheng.shop.Manage;

import com.crecheng.shop.DAO.ItemSpecificData;
import com.crecheng.shop.util.ConSQL;
import com.crecheng.shop.util.ListUtil;

import java.util.List;

public final class ItemSpecificManage {
    public static List<ItemSpecificData> getAll(){
        return ConSQL.select(ItemSpecificData.class);
    }

    public static List<ItemSpecificData> get(String other){
        return ConSQL.select(ItemSpecificData.class,other);
    }

    public static ItemSpecificData get(int id){
        return ListUtil.first(get("id="+id));
    }

    public static List<ItemSpecificData> getByParent(int pid){
        return get("pid="+pid);
    }

    public static List<ItemSpecificData> get(int offset, int num){
        return ConSQL.select(ItemSpecificData.class,offset,num);
    }

    public static List<ItemSpecificData> get(String other,int offset, int num){
        return ConSQL.select(ItemSpecificData.class,other,offset,num);
    }

    public static List<ItemSpecificData> getLikeName(String name,int offset, int num){
        return ConSQL.select(ItemSpecificData.class,"like %"+name+"%",offset,num);
    }

    public static boolean add(ItemSpecificData data){
        return ConSQL.insert(ItemSpecificData.class,data)>0;
    }

    public static boolean del(int id){
        return ConSQL.delete(ItemSpecificData.class,"id="+id)>0;
    }

    public static int delByPid(int pid){
        return ConSQL.delete(ItemSpecificData.class,"pid="+pid);
    }

    public static boolean update(ItemSpecificData data){
        return ConSQL.update(ItemSpecificData.class,data.key(),data)>0;
    }

    public static boolean buy(int id,int count){
        var d=get(id);
        if (d != null) {
            var have=d.getHave();
            var thenHave=have-count;
            if(thenHave<0)
                return false;
            d.setHave(thenHave);
            d.setSale(d.getSale()+count);
            return update(d);
        }
        return false;
    }


}
